/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.util.QiniuUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <dl>
 * <dd>描述: ~ SetmealController</dd>
 * <dd>创建时间：  8:45 2021/2/27</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/27      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {

    @DubboReference
    private SetmealService setmealService;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Author: guodong
     * @Date: 8:48 2021/2/27
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: [套餐-分页查询
    */
    @GetMapping("findPage") //http://localhost:9002/setmeal/findPage?pageSize=10
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[套餐-分页查询]data: {}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    /**
     * @Author: guodong
     * @Date: 9:47 2021/2/27
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 上传图片
    */
    @PostMapping("upload")
    public Result upload(@RequestParam("imgFile") MultipartFile file){
        /*
            1.上传到七牛云
            2.上传完毕后将图片url地址返回
         */
        //用户上传后的原始文件名称
        String originalFilename = file.getOriginalFilename();
        log.info("[图片上传-原始文件名称] {}", originalFilename);
        //自己构造不重复的文件名称
        String fileName = System.currentTimeMillis() + "_" + UUID.randomUUID().toString().replace("-", "") + "_" + originalFilename;
        log.info("[图片上传-改造后不重复文件名称] {}", fileName);
        try {
            //调用七牛云工具类完成图片上传
            qiniuUtils.upload2Qiniu(file.getInputStream(), fileName);

            //上传完毕存放到redis set 集合中
            redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).add(fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConst.PIC_UPLOAD_FAIL);
        }
        String imgUrl = qiniuUtils.getUrlPrefix() + fileName;
        log.info("[图片上传-上传完毕后url地址] {}", imgUrl);
        return new Result(true, MessageConst.PIC_UPLOAD_SUCCESS, imgUrl);
    }

    /**
     * @Author: guodong
     * @Date: 10:07 2021/2/27
     * @Parms [checkgroupIds, setmeal]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 套餐管理-添加套餐
    */
    @PostMapping("add")
    public Result add(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
        log.info("[套餐管理-添加套餐]checkgroupIds : {}, setmeal： {}", checkgroupIds, setmeal);
        //将域名前缀替换为空
        String fileName = setmeal.getImg().replace(qiniuUtils.getUrlPrefix(), "");
        setmeal.setImg(fileName);
        setmealService.add(checkgroupIds, setmeal);
        //新增后将redis set 集合中图片移除
        redisTemplate.boundSetOps(RedisConst.PIC_REDIS_PRE).remove(fileName);
        return new Result(true, MessageConst.ADD_SETMEAL_SUCCESS);
    }
}
